package cn.puge.mertisfront.service.impl;

import cn.puge.mertisfront.entity.PugeContents;
import cn.puge.mertisfront.entity.PugeMer;
import cn.puge.mertisfront.entity.PugeTeamFzr;
import cn.puge.mertisfront.entity.PugeUser;
import cn.puge.mertisfront.entity.queryVo.MerQueryVo;
import cn.puge.mertisfront.mapper.PugeMerMapper;
import cn.puge.mertisfront.service.PugeContentsService;
import cn.puge.mertisfront.service.PugeMerService;
import cn.puge.mertisfront.service.PugeTeamFzrService;
import cn.puge.mertisfront.service.PugeUserService;
import cn.puge.mertisfront.utils.mailUtils;
import cn.puge.servicebase.exceptionhandler.PuGeiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LIJW
 * @since 2020-08-28
 */
@Service
public class PugeMerServiceImpl extends ServiceImpl<PugeMerMapper, PugeMer> implements PugeMerService {

    @Autowired
    private PugeUserService pugeUserService;

    @Autowired
    private IMailService iMailService;

    @Autowired
    private PugeTeamFzrService pugeTeamFzrService;

    @Autowired
    private PugeContentsService pugeContentsService;

    //分页条件查询当前成员所有申报
    @Override
    public List<PugeMer>  getAllMertisQuery( MerQueryVo merQueryVo, String userId) {

        QueryWrapper<PugeMer> queryWrapper = new QueryWrapper<>();
        //根据用户id查询
        queryWrapper.eq("userid",userId);
        queryWrapper.orderByDesc("gmt_create");
         //条件拼接  StringUtils为spring的包，判断字符是否为空
        if(!StringUtils.isEmpty(merQueryVo.getPugeTitle())){
            queryWrapper.like("puge_title",merQueryVo.getPugeTitle());
        }

        if(!StringUtils.isEmpty(merQueryVo.getStatus())){
            queryWrapper.eq("status",merQueryVo.getStatus());
        }
        //查询
        List<PugeMer> pugeMers = baseMapper.selectList(queryWrapper);
        return pugeMers;
    }


    //申报考评,并给队长发邮件去审核 并加入到审核过渡表中
    @Override
    public void addMer(PugeMer pugeMer) {

        /**
         * 判断是否为空
         */
        if (StringUtils.isEmpty(pugeMer.getUserid())||StringUtils.isEmpty(pugeMer.getPugeTitle())||StringUtils.isEmpty(pugeMer.getProve())
               ){
            throw new PuGeiException(20001,"提交的绩效申报为空");
        }

        /**
         * 给成员发邮件信息
         */
        //根据获取到用户id查询出邮箱
        String id = pugeMer.getUserid();
        QueryWrapper<PugeUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        PugeUser one = pugeUserService.getOne(queryWrapper);
        PugeContents pugeContents = pugeContentsService.getById(pugeMer.getPugeTitle());
        //给成员发反馈消息
        iMailService.sendHtmlMail(one.getEmail(),"申请绩效反馈",mailUtils.getContentCy(one, pugeContents.getConame()));

        /**
         * 给队长发邮箱消息
         */
        QueryWrapper<PugeTeamFzr> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id",one.getTeam());
        PugeTeamFzr one1 = pugeTeamFzrService.getOne(queryWrapper1);
        PugeContents pugeContents1 = pugeContentsService.getById(pugeMer.getPugeTitle());
        //给队长发反馈消息
        iMailService.sendHtmlMail(one1.getEmail(),"成员绩效审核通知",mailUtils.getContentDz(one1,one,pugeContents.getConame()));
        /**
         * 把申报信息加入到绩效申报过渡表中
         */


        baseMapper.insert(pugeMer);
    }
}
